<template>
  <div class="app-container">
    <!-- height="300"固定表头，border显示竖直边框 ,stripe显示斑马纹 -->
    <div class="wx-user-box">
      <div class="wx-user-title">微信用户列表</div>
      <div class="search-box">
        <el-form :inline="true" :model="formInline" style="margin-left:20px" label-width="80px">
          <el-form-item label="微信昵称">
            <el-input v-model="formInline.nickname" clearable="" placeholder="请输入应用名称" style="width:200px" @change="search" />
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="formInline.sex" filterable clearable placeholder="请选择" style="width:200px" @change="selectState">
              <el-option
                v-for="item in stateList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="公众号">
            <el-select v-model="formInline.appConfigId" filterable clearable placeholder="请选择" style="width:200px" @change="selectType">
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.app_title"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="margin-left:10px;" @click="search" icon="el-icon-search">查询</el-button>
            <el-button type="warning" @click="syncUser" icon="el-icon-upload">微信同步粉丝</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="wx-user-table-div">
        <el-table v-loading="loading" :data="tableData" border style="width: 100%">
          <el-table-column prop="id" label="ID" width="100" />
          <el-table-column label="头像" width="160">
            <template slot-scope="scope">
              <div class="head-box">
                <img class="headimg" :src="scope.row.headimgurl">
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="nickname" label="昵称" width="320" />
          <el-table-column label="性别" width="120">
            <template slot-scope="scope">
              <span v-if="scope.row.sex==0">未知</span>
              <span v-if="scope.row.sex==1">男</span>
              <span v-if="scope.row.sex==2">女</span>
            </template>
          </el-table-column>
          <el-table-column label="区域" width="300">
            <template slot-scope="scope">
              <span>{{ scope.row.country }}</span>
              <span>{{ scope.row.province }}</span>
              <span>{{ scope.row.city }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="use_nums" label="是否关注" width="140">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.subscribe==0" type="warning" size="mini" effect="dark">未关注</el-tag>
              <el-tag v-else-if="scope.row.subscribe==1" size="mini" effect="dark">已关注</el-tag>
              <el-tag v-else type="danger" size="mini" effect="dark">未知</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="subscribe_time" label="关注时间" />
        </el-table>
      </div>
      <div class="wx-user-pagination-div">
        <el-pagination
          background
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :pager-count="pageCount"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import { getUserList, syncUser } from '@/api/wx'

export default {
  directives: { waves },
  data() {
    return {
      tableData: [], // 列表数据
      pageSize: 10, // 每页条数
      pageCount: 7, //
      pageSizes: [10, 20, 30, 40, 50], // 个数选择器
      currentPage: 1, // 当前页码
      total: 0, // 总条数
      loading: false,
      formInline: {
        nickname: '', // 微信昵称
        appConfigId: '', // 公众号类型
        sex: '' // 性别
      },
      typeList: [],
      stateList: [
        { id: 0, name: '未知' },
        { id: 1, name: '男' },
        { id: 2, name: '女' }
      ]
    }
  },
  computed: {},
  mounted() {
    this.getData(this.currentPage, this.pageSize)
  },
  methods: {

    // 每页显示的条数
    handleSizeChange(val) {
      this.pageSize = val
      this.getData(1, val)
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1
    },

    // 显示第几页
    handleCurrentChange(val) {
      this.currentPage = val
      // 切换页码时，要获取每页显示的条数
      this.getData(val, this.pageSize)
    },

    // 获取微信用户列表
    getData(page, size) {
      const param = {
        'nickname': this.formInline.nickname,
        'appConfigId': this.formInline.appConfigId,
        'sex': this.formInline.sex,
        'pageNumber': page,
        'pageSize': size
      }
      // console.log(param)
      getUserList(param).then(res => {
        // console.log(res)
        if (res.state) {
          this.tableData = res.data.pageData.list // 数据
          this.total = res.data.pageData.totalRow // 总页码
          this.typeList = res.data.wxAppConfigs
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 微信同步粉丝
    syncUser() {
      if (!this.formInline.appConfigId) {
        this.$confirm('请选择公众号!', '提示信息', {
          confirmButtonText: '确定',
          showCancelButton: false,
          type: 'warning'
        }).then(() => {
        }).catch(() => {})
        return
      }
      syncUser({ id: this.formInline.appConfigId }).then(res => {
        if (res.state) {
          this.$message({ message: res.msg, type: 'success', duration: 2000 })
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 选择公众号类型
    selectType(e) {
      // console.log(e)
      this.formInline.appConfigId = e
      this.getData(this.currentPage, this.pageSize)
    },

    // 选择性别
    selectState(e) {
      // console.log(e)
      this.formInline.sex = e
      this.getData(this.currentPage, this.pageSize)
    },

    // 搜索
    search() {
      this.getData(this.currentPage, this.pageSize)
    }

  }
}
</script>

<style lang="scss" scoped>
.wx-user-box{
  border: 1px solid #f2f2f2;
  border-radius: 3px;
  box-shadow: 0 1px 1px 0 rgba(0,0,0,.12);
  background: #fff;

  .wx-user-title{
    font-size: 16px;
    color: #333;
    padding: 15px;
    border-bottom: 1px solid #f2f2f2;
  }

  .search-box{
    padding: 20px 15px;
  }

  .wx-user-table-div{
    padding: 15px;

    .head-box{
      display: flex;
      align-items: center;

      .headimg{
        width: 34px;
        height: 34px;
        border-radius: 100%;
      }
    }
  }

  .wx-user-pagination-div{
     padding: 10px 0;
     border-top: 1px solid #dfe6ec;
     margin: 0 15px;
  }

}
</style>

<style lang="scss">
.search-box{
   label{
    font-weight: 500;
  }
  .el-form-item__label{
    font-size: 15px;
    color: #333;
  }
}
</style>
